#include "PlayOnWithBallBehaviour.h"

// Zoznam informacii o hracoch, ktorych vidi hrac
extern CEvaluatingPlayerList* ePlayerList;

/*
 * Spravanie hraca pocas normalneho hracieho modu.
 * Jednoducho hrac uteka s loptou niekde po ihrisku...
 */
void PlayOnWithBallBehaviour::Behave()
{
	Player* enemyGoalie = DATA_STORAGE.GetEnemyGoalie();

	// <Strelba na branu>
	if(GetKickToGoalBehaviour()->ShouldKickOnGoal())
	{
		GetKickToGoalBehaviour()->Behave();
		return;
	}
	else if(DATA_STORAGE.DistanceToGoal(DATA_STORAGE.myPosition) < 17.0)
	{
		GetKickToGoalBehaviour()->Behave();
		return;
	}
	// Preventivne vykopnutie lopty zo zony ohrozenia
/*		else if(myPosition.GetX() < -45.0)
	POZNAMKA: Ak je hrac otoceny smerom k nasej branke, tak chybou Dribble2, alebo DumbKick si hrac niekedy da vlastny gol 
	Odporucam nepouzivat toto, pokial sa ta chyba neodstrani.
	{
		GetPassToStrategicPositionBehaviour()->Behave();
		return;
	}
*/		// <Nahravanie>
	else if( ((DATA_STORAGE.GetOffsideLine() - DATA_STORAGE.myPosition.GetX()) < 4.75f) && 
		(world->me->GetUniform() >= 8) && (world->ball->pos.GetX() < 33.0f) )
	{
		GetOffsideLineBreachBehaviour()->Behave(DATA_STORAGE.GetOffsideLine());
		return;
	}
	else if(ePlayerList->CalculateNormalPassPosition(DATA_STORAGE.myPosition.GetX(), DATA_STORAGE.myPosition.GetY(), 
		DATA_STORAGE.lfTmpX, DATA_STORAGE.lfTmpY))
	{
		// <Prioritny dribling>
		if((ePlayerList->NoEnemiesAround(DATA_STORAGE.myPosition)) && (DATA_STORAGE.myPosition.GetX() <= 30))
		{
			if(!GetDribbleBehaviour()->ManageDribblePoint())
			{	
				GetPassToStrategicPositionBehaviour()->Behave();
				return;
			}
			else
			{
				GetDribbleBehaviour()->Behave(Point((float)DATA_STORAGE.lfDribbleX, (float)DATA_STORAGE.lfDribbleY), 0, true);
				
//					log1 << "Vypocitany driblovaci bod: (" << DATA_STORAGE.lfDribbleX << ", " << DATA_STORAGE.lfDribbleY << ")"  << endl;
//					log1 << "DribbleBehaviour()" << endl;
//					log1.close();

				//	Ak nevidim brankara a uz by som ho mal vidiet, skus ho najst otocenim krku
				if(DATA_STORAGE.DistanceToGoal(DATA_STORAGE.myPosition) <= 25)
				{
					GetTurnNeckToBehaviour()->Behave(Point(38, 0), world->ball->pos, 0);
//						log1 << "TurnNeckTo(Point(38, 0))" << endl;
//						log1.close();
				}
				
				return;
			}
		}
		else
		{
			GetPassBallToNormalPassPositionBehaviour()->Behave();
			return;
		}
	}
	// <Dribling>
	else
	{
		// Ak je brankar nebezpecne blizko a strielat nie je vhodne
		if(enemyGoalie != NULL && (enemyGoalie->pos - world->ball->pos).GetLength() < 5.f)
		{	
			GetPassBallNearToGoalieBehaviour()->Behave();
			return;
		}

/*			else if(IsPointInRectArea(myPosition.GetX(), myPosition.GetY(), 32.0, -20.16, 52.5, 20.16))
		{
			LPT_KickToGoal();
//				log1 << "LPT_KickToGoal()" << endl;
//				log1.close();

			return;
		}
*/		
		// Ak som hlboko v pasme supera a v obrannom pasme supera a v obrannom pasme supera su nejaki moji hraci
		else if((IsPointInRectArea(DATA_STORAGE.myPosition.GetX(), DATA_STORAGE.myPosition.GetY(), 
			25.0, -25.0, 52.5, 25.0)) && (ePlayerList->OurPlayersInEnemyPenaltyArea()))
		{
			GetCrossPassToGoalBehaviour()->Behave();
			return;
		}

		// Ak je pri mne NEARBY hrac, tak sa lopty treba zbavit
		else if(ePlayerList->NearbyEnemiesAround(DATA_STORAGE.myPosition))
		{
			if(!GetDribbleBehaviour()->ManageDribblePoint())
			{	
				GetPassToStrategicPositionBehaviour()->Behave();
				return;
			}
			else
			{
				GetDribbleBehaviour()->Behave(Point((float)DATA_STORAGE.lfDribbleX, (float)DATA_STORAGE.lfDribbleY), 0);

//					log1 << "Vypocitany driblovaci bod: (" << DATA_STORAGE.lfDribbleX << ", " << DATA_STORAGE.lfDribbleY << ")"  << endl;
//					log1 << "DribbleBehaviour" << endl;
//					log1.close();

				//	Ak nevidim brankara a uz by som ho mal vidiet, skus ho najst otocenim krku
				if(DATA_STORAGE.DistanceToGoal(DATA_STORAGE.myPosition) <= 35)
				{
					GetTurnNeckToBehaviour()->Behave(Point(40, 0), world->ball->pos, 0);
//								log1 << "TurnNeckTo(Point(40, 0))" << endl;	
//								log1.close();
				}
				return;
			}
		}
		else
		{
			if(DATA_STORAGE.GetNearestEnemyDistance(DATA_STORAGE.myPosition) >= 5.0f)
			{
				if(!GetDribbleBehaviour()->ManageDribblePoint())
				{	
					GetPassToStrategicPositionBehaviour()->Behave();
					//OutPlayOpponent();
					return;
				}
				else
				{
					GetDribbleBehaviour()->Behave(Point((float)DATA_STORAGE.lfDribbleX, (float)DATA_STORAGE.lfDribbleY), 0);

//						log1 << "Vypocitany driblovaci bod: (" << DATA_STORAGE.lfDribbleX << ", " << DATA_STORAGE.lfDribbleY << ")"  << endl;
//						log1 << "DribbleBehaviour" << endl;
//						log1.close();

					//	Ak nevidim brankara a uz by som ho mal vidiet, skus ho najst otocenim krku
					if(DATA_STORAGE.DistanceToGoal(DATA_STORAGE.myPosition) <= 35)
					{
						GetTurnNeckToBehaviour()->Behave(Point(40, 0), world->ball->pos, 0);
//							log1 << "TurnNeckTo(Point(40, 0))" << endl;	
//							log1.close();
					}
					return;
				}
			}
			else
			{
				if(DATA_STORAGE.GetNearestEnemyDistance(DATA_STORAGE.myPosition) <= 1.5f)
				{
					GetPassToStrategicPositionBehaviour()->Behave();
					return;
				}
				else
				{
					GetOutPlayOpponentBehaviour()->Behave();
					return;
				}
			}
		}
	}	
}
